package co.edu.usbcali.lidis.vista;

import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.List;

import co.edu.usbcali.lidis.modelo.Usuario;

public class Inicio{
	public Inicio() {
	}

	protected Shell shell;
	private Text txtUsuario;
	private Text txtPassword;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Inicio window = new Inicio();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(398, 235);
		
		Label lblUsuario = new Label(shell, SWT.NONE);
		lblUsuario.setBounds(71, 47, 55, 15);
		lblUsuario.setText("Usuario:");
		
		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setBounds(71, 80, 71, 15);
		lblPassword.setText("Contraseña:");
		
		txtUsuario = new Text(shell, SWT.BORDER);
		txtUsuario.setBounds(148, 44, 143, 21);
		
		txtPassword = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		txtPassword.setBounds(147, 77, 144, 21);
		
		Button btnIngresar = new Button(shell, SWT.NONE);
		btnIngresar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					if (txtUsuario.getText().trim().equals("") == true) {
						throw new Exception("Por favor ingrese un usuario");
					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e.getMessage(),
							"Error", JOptionPane.ERROR_MESSAGE);
				}
				
				Usuario usu = new Usuario();
				usu.setUsuUsuario(txtUsuario.getText());
				usu.setUsuPassword(txtPassword.getText());
				
				try {
					if (BusinessDelegate.getInstance().inicioSesion(usu)) {
							Prueba pantallaProyecto = new Prueba();
							pantallaProyecto.setVisible(true);	
							shell.close();
					} else {
						System.out.println("Datos invalidos");
						JOptionPane.showMessageDialog(null, "Datos inválidos.",
								"Error", JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnIngresar.setBounds(155, 118, 75, 25);
		btnIngresar.setText("Ingresar");

	}
}
